<?php
require_once 'CMS/Util.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseUserController.php';
require_once 'CMS/UserCommon.php';
require_once 'CMS/Language.php';

//Người tạo : Hoàng Hải Luân - Team 4
//Module 	: Language
//Chức năng	: Show language
 
class Language_IndexController {

	public function init(){
		parent::init();
	}

	public function indexAction(){
		$tbLanguage = new Language_Model_Language();
		$languages = $tbLanguage->getAllLanguages();
		$id_default = 0;
		$default = $tbLanguage->getDefaultLanguage();
		if(isset($default) && count($default)){
				$id_default = $default['language_id'];
		}
		
		if(!isset($_SESSION['language']) && $id_default != 0)
		{
			$_SESSION['language'] = $id_default;
			$this->view->id_default = $id_default;
		}
		elseif(isset($_SESSION['language']))
		{
			$check = $tbLanguage->getNameLanguage($_SESSION['language']);
			if(count($check) > 0)
				$this->view->id_default = $_SESSION['language'];
			else
			{
				$_SESSION['language'] = $id_default;
				$this->view->id_default = $id_default;				
			}
		}
		else
			$this->view->id_default = $id_default;
				
		$this->view->languages	= $languages;
		$this->view->error_msg  = "Changing language has error.Please try again !";
		
		/*========== Start Load Language ==========*/
		$lang = new Language(APPLICATION_PATH . '/widgets/language');
		if(isset($_SESSION['language']))
		{
			$language_id = $_SESSION['language'];
			$tbLanguage = new Language_Model_Language();
			$name = $tbLanguage->getNameLanguage($language_id);
			$lang->load($name['name']);
		}
		else
			$lang->load('Default');		
		/*========== End Load Language ==========*/
		
		
		/*========== Start Get Text ==============*/
		$this->view->textLang		= $lang->get('text_lang');
		/*========== End Text ==============*/
	}

	

}